package com.xuchongyang.easylinphone;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.xuchongyang.easyphone.EasyLinphone;
import com.xuchongyang.easyphone.callback.PhoneCallback;
import com.xuchongyang.easyphone.callback.RegistrationCallback;
import com.xuchongyang.easyphone.service.LinphoneService;

import org.apache.cordova.*;

import org.json.JSONArray;
import org.json.JSONException;
import org.linphone.core.LinphoneCall;

/**
 * This class echoes a string called from JavaScript.
 */
public class Linphone extends CordovaPlugin {

    public static Context mContext;
    private CallbackContext callbackContext;

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        mContext = cordova.getActivity().getApplicationContext();
        if (!LinphoneService.isReady()) {
            EasyLinphone.startService(mContext);
            EasyLinphone.addCallback(new RegistrationCallback() {
                @Override
                public void registrationOk() {
                    super.registrationOk();
                }

                @Override
                public void registrationFailed() {
                    super.registrationFailed();
                }
            }, null);

        }

        EasyLinphone.addCallback(null, new PhoneCallback() {
            @Override
            public void incomingCall(LinphoneCall linphoneCall) {
                super.incomingCall(linphoneCall);
                // 开启铃声免提
                EasyLinphone.toggleSpeaker(true);
                EasyLinphone.acceptCall();
                goVideoActivity();
            }
            @Override
            public void outgoingInit() {
                super.outgoingInit();
            }

            @Override
            public void callConnected() {
                super.callConnected();
                // 视频通话默认免提，语音通话默认非免提
                EasyLinphone.toggleSpeaker(EasyLinphone.getVideoEnabled());
                // 所有通话默认非静音
                EasyLinphone.toggleMicro(false);
            }

            @Override
            public void callEnd() {
                super.callEnd();
                mContext.sendBroadcast(new Intent(VideoActivity.RECEIVE_FINISH_VIDEO_ACTIVITY));
            }
        });
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("login")) {
            login(args.getString(0), args.getString(1), args.getString(2), callbackContext);
            return true;
        } else if (action.equals("logout")) {
//            logout(callbackContext);
            return true;
        } else if (action.equals("audioCall")) {
            audioCall(args.getString(0), callbackContext);
            return true;
        } else if (action.equals("videoCall")) {
            videoCall(args.getString(0), callbackContext);
            return true;
        } else if (action.equals("acceptCall")) {
            acceptCall(args.getString(0), callbackContext);
            return true;
        } else if (action.equals("listenCall")) {
//            videocall(args.getString(0), args.getString(1), callbackContext);
            return true;
        } else if (action.equals("hangUp")) {
            hangUp(callbackContext);
            return true;
        } else if (action.equals("toggleSpeaker")) {
            toggleSpeaker(callbackContext);
            return true;
        } else if (action.equals("toggleMute")) {
            toggleMute(callbackContext);
            return true;
        }
        return false;
    }

    private static void goVideoActivity() {
        Intent intent = new Intent(mContext, VideoActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    public static synchronized void login(final String username, final String password, final String domain, final CallbackContext callbackContext) {
        try {
            EasyLinphone.setAccount(username, password, domain);
            EasyLinphone.login();
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
        } catch (Exception e) {
            Log.e("login error", e.getMessage());
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.getMessage()));
        }
    }

    public static synchronized void audioCall(final String number, final CallbackContext callbackContext) {
        try {
            EasyLinphone.callTo(number, false);
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
        } catch (Exception e) {
            Log.e("audioCall error", e.getMessage());
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.getMessage()));
        }
    }

    public static synchronized void videoCall(final String number, final CallbackContext callbackContext) {
        try {
            EasyLinphone.callTo(number, true);
            goVideoActivity();
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
        } catch (Exception e) {
            Log.e("videoCall error", e.getMessage());
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.getMessage()));
        }
    }

    public static synchronized void acceptCall(final String number, final CallbackContext callbackContext) {
        try {
            EasyLinphone.acceptCall();
            if (EasyLinphone.getVideoEnabled()) {
                goVideoActivity();
            }
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
        } catch (Exception e) {
            Log.e("acceptCall error", e.getMessage());
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.getMessage()));
        }
    }

    public static synchronized void hangUp(final CallbackContext callbackContext) {
        try {
            EasyLinphone.hangUp();
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
        } catch (Exception e) {
            Log.e("hangUp error", e.getMessage());
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.getMessage()));
        }
    }

    public static synchronized void toggleSpeaker(final CallbackContext callbackContext) {
        try {
            EasyLinphone.toggleSpeaker(!EasyLinphone.getLC().isSpeakerEnabled());
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
        } catch (Exception e) {
            Log.e("toggleSpeaker error", e.getMessage());
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.getMessage()));
        }
    }

    public static synchronized void toggleMute(final CallbackContext callbackContext) {
        try {
            EasyLinphone.toggleMicro(!EasyLinphone.getLC().isMicMuted());
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
        } catch (Exception e) {
            Log.e("toggleMute error", e.getMessage());
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.getMessage()));
        }
    }


}
